﻿//
// CreateItemViewModel.cs
//
// Authors:
//    Anders Gade Nørskov <09549@iha.dk
//    Lars Rønde Hansen <09548@iha.dk>
//    Claus Jørgensen <10229@iha.dk>
//
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Input;

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace RSD.ViewModel
{
    using Data;
    using ViewModel.Messages;

    public class CreateItemClassificationViewModel : ViewModelBase
    {
        private readonly IItemClassificationRepository itemClassificationRepository;
        private readonly RelayCommand createItemCommand;
        private readonly IPositionRepository positionRepository;

        private double maxDensity;
        private string description;
        private double minDensity;
        private Position selectedPosition;

        public CreateItemClassificationViewModel(
            IItemClassificationRepository itemClassificationRepository,
            IPositionRepository positionRepository)
        {
            this.itemClassificationRepository = itemClassificationRepository;
            this.positionRepository = positionRepository;

            this.Positions = new ObservableCollection<Position>(positionRepository.FindAll());
            this.createItemCommand = new RelayCommand(CreateItemExecute);
        }

        public ICommand CreateItemCommand
        {
            get
            {
                return createItemCommand;
            }
        }

        public ObservableCollection<Position> Positions
        {
            get;
            set;
        }

        public Position SelectedPosition
        {
            get
            {
                return selectedPosition;
            }
            set
            {
                selectedPosition = value;
                RaisePropertyChanged("SelectedPosition");
            }
        }

        public double MinDensity
        {
            get
            {
                return minDensity;
            }

            set
            {
                minDensity = value;
                RaisePropertyChanged("MinDensity");
            }
        }

        public double MaxDensity
        {
            get
            {
                return maxDensity;
            }
            set
            {
                maxDensity = value;
                RaisePropertyChanged("MaxDensity");
            }
        }

        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
                RaisePropertyChanged("Description");
            }
        }

        private void CreateItemExecute()
        {
            var itemClassification = new ItemClassification()
            {
                MinDensity = minDensity,
                MaxDensity = maxDensity,
                Description = description,
                Position = selectedPosition
            };

            Messenger.Default.Send(new ItemClassificationCreatedMessage(itemClassification));
        }
    }
}
